package todolist.activities;

import java.util.List;

import todolist.activities.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * Shows the login screen
 * If new account button is clicked, changes to create new account screen 
 *
 */
public class FilterActivity extends Activity implements OnClickListener {
    
	TodoList context;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.filter);
        
        context = ((TodoList) getApplicationContext());
        
        //definitions for buttons and checkboxes
        Button personalButton = (Button) findViewById(R.id.btnPersonal);
        Button schoolButton = (Button) findViewById(R.id.btnSchool);
        Button workButton = (Button) findViewById(R.id.btnWork);
        Button allButton = (Button) findViewById(R.id.btnAll);
        CheckBox completeBox = (CheckBox) findViewById(R.id.checkbox);
        completeBox.setChecked(context.isShowComplete());
        
        EditText dateText  = (EditText) findViewById(R.id.dateText);
        dateText.setText(context.getFilterDate());
        
        CheckBox dateBox = (CheckBox) findViewById(R.id.dateBox);
        dateBox.setChecked(context.isFilterByDate());
        
        
        //onClick listeners for all buttons
        personalButton.setOnClickListener(this);
        schoolButton.setOnClickListener(this);
        workButton.setOnClickListener(this);
        allButton.setOnClickListener(this);

	}

	// Will be called via the onClick attribute
	// of the buttons in filter.xml
	public void onClick(View v) {

		String date = "";
		switch (v.getId()) {

		case R.id.btnPersonal:
			context.setTypesToShow("personal");
    		date = ((EditText) findViewById(R.id.dateText)).getText().toString();
			context.setFilterDate(date);
			finish();
			break;

		case R.id.btnSchool:
			context.setTypesToShow("school");
    		date = ((EditText) findViewById(R.id.dateText)).getText().toString();
			context.setFilterDate(date);
			finish();
			break;
		
		
		case R.id.btnWork:
			context.setTypesToShow("work");
    		date = ((EditText) findViewById(R.id.dateText)).getText().toString();
			context.setFilterDate(date);
			finish();
			break;
		
		case R.id.btnAll:
			context.setTypesToShow("all");
    		date = ((EditText) findViewById(R.id.dateText)).getText().toString();
			context.setFilterDate(date);
			finish();
			break;
	}

	}
	
	public void onCheckboxClicked(View v) {
	    // Perform action on clicks, depending on whether it's now checked
		
		//This code executes if the show completed items checkbox is checked
	    if (((CheckBox) v).isChecked()) {
	    	if (((CheckBox) v).equals((CheckBox) findViewById(R.id.checkbox)))
	    	{
	    		Toast.makeText(FilterActivity.this, "Showing completed and uncompleted items", Toast.LENGTH_SHORT).show();
	    		context.setShowComplete(true);
	    	}
	    	else
	    	{
	    		String date = ((EditText) findViewById(R.id.dateText)).getText().toString();

    			Toast.makeText(FilterActivity.this, "Showing only items after " + date, Toast.LENGTH_SHORT).show();
    			context.setFilterDate(date);
    			context.setFilterByDate(true);
	    	}
	    } 
	    
	    //If it is the case that the show complete items is not checked this code executes
	    else {
	    	if (((CheckBox) v).equals((CheckBox) findViewById(R.id.checkbox)))
	    	{
	    		Toast.makeText(FilterActivity.this, "Showing only uncompleted items", Toast.LENGTH_SHORT).show();
	        	context.setShowComplete(false);
	    	}
	    	else
	    	{
	    		Toast.makeText(FilterActivity.this, "Not filtering by date", Toast.LENGTH_SHORT).show();
	    		context.setFilterByDate(false);
	    	}
	    }
	}
	

}